#import "SMRSubtleScroller.h"
#import "NSShadow+MCAdditions.h"
#import "NSBezierPath+SMRAdditions.h"


#pragma mark Constants
static const CGFloat SMRSubtleScrollerKnobWidth = 10.0f;
static const CGFloat SMRSubtleScrollerKnobCornerRadius = 4.0f;


#pragma mark -
#pragma mark Class Globals

static NSShadow *SMRSubtleScrollerSlotInnerShadow;
static NSColor *SMRSubtleScrollerSlotBackgroundColor;
static NSColor *SMRSubtleScrollerBorderColor;
static NSGradient *SMRSubtleScrollerKnobBackgroundGradient;

static NSShadow *SMRSubtleScrollerSlotInnerShadowSelected;
static NSColor *SMRSubtleScrollerSlotBackgroundColorSelected;
static NSColor *SMRSubtleScrollerBorderColorSelected;
static NSGradient *SMRSubtleScrollerKnobBackgroundGradientSelected;


#pragma mark -
@implementation SMRSubtleScroller

@synthesize selected;


+(void)initialize {
	NSColor *shadowColor = [NSColor colorWithCalibratedWhite:0.5 alpha:0.4];
	NSColor *shadowColorSelected = [NSColor colorWithCalibratedWhite:0.0 alpha:0.3];
	SMRSubtleScrollerSlotInnerShadow = [[NSShadow alloc] mcInitWithColor:shadowColor offset:NSMakeSize(1.0f, -1.0f) blurRadius:2.0f];
	SMRSubtleScrollerSlotInnerShadowSelected = [SMRSubtleScrollerSlotInnerShadow copy];
	[SMRSubtleScrollerSlotInnerShadowSelected setShadowColor:shadowColorSelected];
	
	SMRSubtleScrollerSlotBackgroundColor = [[NSColor colorWithCalibratedWhite:0.6f alpha:0.1f] retain];
	SMRSubtleScrollerSlotBackgroundColorSelected = [[NSColor colorWithCalibratedRed:0.53 green:0.65 blue:0.78 alpha:1.000] retain];
	
	SMRSubtleScrollerBorderColor = [[NSColor colorWithCalibratedWhite:0.750 alpha:1.000] retain];
	SMRSubtleScrollerBorderColorSelected = [[NSColor colorWithCalibratedRed:0.387 green:0.434 blue:0.501 alpha:1.000] retain];
	
	NSColor *knobRightColor = [NSColor colorWithCalibratedWhite:0.96 alpha:1.0f];
	NSColor *knobLeftColor = [NSColor colorWithCalibratedWhite:1.0f alpha:1.0f];
	SMRSubtleScrollerKnobBackgroundGradient = [[NSGradient alloc] initWithStartingColor:knobLeftColor endingColor:knobRightColor];
	
	NSColor *knobRightColorSelected = [NSColor colorWithCalibratedRed:0.61 green:0.688 blue:0.809 alpha:1.000];
	NSColor *knobLeftColorSelected = [NSColor colorWithCalibratedRed:0.643 green:0.745 blue:0.874 alpha:1.000];
	SMRSubtleScrollerKnobBackgroundGradientSelected = [[NSGradient alloc] initWithStartingColor:knobLeftColorSelected endingColor:knobRightColorSelected];
}

+(CGFloat)scrollerWidth {
	return SMRSubtleScrollerKnobWidth;
}

+(CGFloat)scrollerWidthForControlSize:(NSControlSize)controlSize {
	return SMRSubtleScrollerKnobWidth;
}


#pragma mark -
#pragma mark Initialization

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self setArrowsPosition:NSScrollerArrowsNone];
	return self;
}

-(id)initWithFrame:(NSRect)frameRect {
	if (!(self = [super initWithFrame:frameRect]))
		return nil;
	[self setArrowsPosition:NSScrollerArrowsNone];
	return self;
}


#pragma mark -
#pragma mark Overrides

-(BOOL)isOpaque {
	return NO;
}

-(NSScrollArrowPosition)arrowsPosition {
	return NSScrollerArrowsNone;
}

-(NSRect)rectForPart:(NSScrollerPart)aPart {
	NSRect partRect = [super rectForPart:aPart];
	CGFloat widthDelta = partRect.size.width - SMRSubtleScrollerKnobWidth;
	partRect.origin.x += widthDelta;
	partRect.size.width = SMRSubtleScrollerKnobWidth;
	return partRect;
}


#pragma mark -
#pragma mark Drawing

-(void)drawRect:(NSRect)dirtyRect {
	[self drawKnobSlotInRect:[self rectForPart:NSScrollerKnobSlot] highlight:NO];
	[self drawKnob];
}

-(void)drawKnob {
	NSRect knobRect = [self rectForPart:NSScrollerKnob];
	if (knobRect.size.width < 1.0f || knobRect.size.height < 1.0f)
		return;
	knobRect = NSInsetRect(knobRect, 0.5, 0.5);
	NSBezierPath *knobPath = [NSBezierPath bezierPathWithRoundedRect:knobRect xRadius:SMRSubtleScrollerKnobCornerRadius yRadius:SMRSubtleScrollerKnobCornerRadius];

	NSGradient *backgroundGradient = [self selected] ? SMRSubtleScrollerKnobBackgroundGradientSelected : SMRSubtleScrollerKnobBackgroundGradient;
	[backgroundGradient drawInBezierPath:knobPath angle:0];

	NSColor *borderColor = [self selected] ? SMRSubtleScrollerBorderColorSelected : SMRSubtleScrollerBorderColor;
	[borderColor setStroke];
	[knobPath stroke];
}

-(void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)isHighlighted {
	if (slotRect.size.width < 1.0f || slotRect.size.height < 1.0f)
		return;
	slotRect = NSInsetRect(slotRect, 0.5, 0.5);
	NSBezierPath *slotPath = [NSBezierPath bezierPathWithRoundedRect:slotRect xRadius:SMRSubtleScrollerKnobCornerRadius yRadius:SMRSubtleScrollerKnobCornerRadius];

	NSColor *backgroundColor = [self selected] ? SMRSubtleScrollerSlotBackgroundColorSelected : SMRSubtleScrollerSlotBackgroundColor;
	[backgroundColor setFill];
	[slotPath fill];

	NSShadow *innerShadow = [self selected] ? SMRSubtleScrollerSlotInnerShadowSelected : SMRSubtleScrollerSlotInnerShadow;
	[slotPath smrFillWithInnerShadow:innerShadow];

	NSColor *borderColor = [self selected] ? SMRSubtleScrollerBorderColorSelected : SMRSubtleScrollerBorderColor;
	[borderColor setStroke];
	[slotPath stroke];
}

@end
